package com.dataart.zipbackup;

import java.io.FileInputStream;
import java.io.FileNotFoundException;
import java.io.FileOutputStream;
import java.io.IOException;
import java.io.InputStream;
import java.io.OutputStream;
import java.util.zip.ZipEntry;
import java.util.zip.ZipOutputStream;

public class ZipProcessorMain {

	public static void main(String[] args) {
		try { // TRY WITH RESOURCES
			TextProcessor textProcessor = new TextProcessor();
			String fileName = "C:\\Users\\zkhaymedinova\\Documents\\DataArt\\ReviewTest\\ZipFolder\\inputs.zip";
			String fileNameOut = "C:\\Users\\zkhaymedinova\\Documents\\DataArt\\ReviewTest\\ZipFolder\\inputs_v2.zip";
			InputStream inputStream = new FileInputStream(fileName);
			OutputStream outputStream = new FileOutputStream(fileNameOut);
			ZipProcessor zipList = new ZipProcessor();
			ZipOutputStream zos;
			zos = zipList.processZip(textProcessor, inputStream, outputStream);
			zos = writePhone(zos, textProcessor);
			zos = writeEmail(zos,textProcessor);
			zos.finish();
			outputStream.close();
			System.out.println("Execution complete!"); // logger. exception here.
		} catch (FileNotFoundException e) {
			e.printStackTrace(); //  VERY BAD NORMALLY 
		} catch (IOException e1){
			e1.printStackTrace();
		}
	}

	private static ZipOutputStream writePhone(ZipOutputStream zos, TextProcessor textProcessor) {
		try {
			ZipEntry zipEntryPhone = new ZipEntry("phones.txt");
			zos.putNextEntry(zipEntryPhone);
			textProcessor.writePhone(zos);
			zos.closeEntry();
		} catch (IOException e) {
			e.printStackTrace();
		}
		return zos; // do not return
		
	}
	
	private static ZipOutputStream writeEmail(ZipOutputStream zos, TextProcessor textProcessor) {
		try {
			ZipEntry zipEntryEmail = new ZipEntry("emails.txt");
			zos.putNextEntry(zipEntryEmail);
			textProcessor.writeEmail(zos);
			zos.closeEntry();
		} catch (IOException e) {
			e.printStackTrace();
		}
		return zos;
		
	}
}